﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MyPlanSilverlight.Utils;

namespace MyPlanSilverlight
{
    public partial class AddCompany : UserControl
    {
        public AddCompany()
        {
            InitializeComponent();
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            string name = companyName.Text.Trim();
            string industry = companyIndustry.Text.Trim();
            string scale = companyScale.Text.Trim();
            string address = companyAddress.Text.Trim();
            string desc = companyDesc.Text.Trim();

            string url = App.Current.Host.Source.ToString();
            url = url.Substring(0, url.IndexOf('/', 7) + 1) + "api/company/add";
            Uri URL = new Uri(url);

            HttpHelper helper = new HttpHelper(URL, "POST",
                new KeyValuePair<string, string>("Name", name),
                new KeyValuePair<string, string>("Industry", industry),
                new KeyValuePair<string, string>("Scale", scale),
                new KeyValuePair<string, string>("Address", address),
                new KeyValuePair<string, string>("Description", desc));
            helper.ResponseComplete += new HttpResponseCompleteEventHandler(this.CommandComplete);
            helper.Execute();
        }

        private void CommandComplete(HttpResponseCompleteEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                Response response = Response.Init(e.Response);

                if (response.Status == ConstanCodes.SUCCESS)
                {
                    MessageBox.Show("新增成功！");
                }
                else if (response.Status == ConstanCodes.SERVER_ERROR)
                {
                    MessageBox.Show("服务器错误");
                }
            });
        }

        private void GoBack_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
